
<template>
    <div class="layout">

        <!-- 判断登录状态，vue dom操作 -->
        <div v-if="!login" class="loginbox" >
            <Modal
                title="登录"
                v-model="loginflag"
                :footer-hide = true
                :mask-closable="false"
                :styles="{top: '20px'}">
                <Form ref="formInline" :model="formInline" :rules="ruleInline">
                <FormItem prop="name">
                    <Input type="text" v-model="formInline.name" placeholder="Username">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <FormItem prop="password">
                    <Input type="password" v-model="formInline.password" placeholder="Password">
                        <Icon type="ios-lock-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
                </FormItem>
            </Form>
            </Modal>
             <!-- <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
                <FormItem prop="name">
                    <Input type="text" v-model="formInline.name" placeholder="Username">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <FormItem prop="password">
                    <Input type="password" v-model="formInline.password" placeholder="Password">
                        <Icon type="ios-lock-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formInline')">Signin</Button>
                </FormItem>
            </Form> -->

        </div>
        <div v-else>
            <Layout >
            <Header >
                <Menu mode="horizontal" theme="dark" active-name="1">
                    <div class="layout-logo">后台管理系统</div>
                    <div class="layout-nav">
                        欢迎回来，{{username}}
                        <MenuItem name="1" @click.native="logout">
                            <Icon type="ios-navigate"></Icon>
                            注销
                        </MenuItem>
                    </div>
                </Menu>
            </Header>
            <Layout >
                <Sider hide-trigger :style="{background: '#fff'}">
                    <Menu active-name="1-1" theme="light" width="auto" :open-names="['1']">
                        <Submenu name="1">
                            <template slot="title">
                                <Icon type="ios-navigate"></Icon>
                                系统功能
                            </template>
                            <MenuItem name="1-1"><router-link to="/"  @click.native="changename">首页</router-link></MenuItem>
                            <MenuItem name="1-2"><router-link to="/users"  @click.native="changename">用户</router-link></MenuItem>
                            <MenuItem name="1-3"><router-link to="/cates"  @click.native="changename">分类</router-link></MenuItem>
                            <MenuItem name="1-4"><router-link to="/news"  @click.native="changename">新闻</router-link></MenuItem>
                            <MenuItem name="1-5"><router-link to="/products"  @click.native="changename">产品</router-link></MenuItem>
                        </Submenu>
                        <Submenu name="2">
                            <template slot="title">
                                <Icon type="ios-keypad"></Icon>
                                说明
                            </template>
                            <MenuItem name="2-1" @click.native="totext" >使用说明</MenuItem>
                        </Submenu>
                        <Submenu name="3">
                            <template slot="title">
                                <Icon type="ios-analytics"></Icon>
                                作者
                            </template>
                            <MenuItem name="3-1" @click.native="showme">联系作者</MenuItem>
                            <Modal
                                title="兼职程序猿的段子手"
                                v-model="author"
                                :styles="{top: '20px'}">
                                <author></author>
                            </Modal>
                        </Submenu>
                    </Menu>
                </Sider>
                <Layout :style="{padding: '0 24px 24px'}">
                    <Breadcrumb :style="{margin: '24px 0'}">
                        <BreadcrumbItem>系统</BreadcrumbItem>
                        <BreadcrumbItem>{{Componentsname}}</BreadcrumbItem>

                    </Breadcrumb>
                    <Content :style="{padding: '24px',maxHeight:'550px', background: '#fff',overflow:'auto'}">
                        <router-view></router-view>
                    </Content>
                </Layout>
            </Layout>
        </Layout>
        </div>

    </div>
</template>
<script>
import author from './views/author';
import { mapGetters,mapActions } from 'vuex';
    export default {
      components: {
            author,
        },
      data() {
        return {
            loginflag:true,
            author:false,
          Componentsname:'首页',
          username:'小布',
          formInline: {
                    name    : '',
                    password: ''
                },
                ruleInline: {
                    name: [
                        { required: true, message: '请输入账号', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ]
                }
        }
      },
      computed: {
           ...mapGetters(['login','token'])
      },
      methods:{
          showme(){
              this.author=true
          },
          //打开说明
          totext(){
              window.location.href="https://gitee.com/leon_best/vue-cli-cms"
          },
          logout(){
              window.location.href="http://localhost:8080/"
              location.reload(true);
          },
        changename(event){
          //console.log(event.target.text);
          this.Componentsname=event.target.text;

        },
        ...mapActions(['checkLogin','setToken']),
         handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        console.log(this.formInline);

                       this.axios({
                           url   : `${this.GLOBAL.serverurl}users/login`,
                           method: 'post',
                           data:this.formInline
                       }).then(res=>{
                           //console.log(res.data)

                           if(!res.data.status&&res.data.status!=401){
                                this.setToken(res.data.token);
                                this.checkLogin(true);
                           }else{
                               //账号密码错误提示
                               this.$Message.error('您输入的账号密码不正确');
                           }

                       })
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            },
      },
      mounted() {
          console.log(this.GLOBAL)
      },
    }
</script>
<style scoped>

.layout{
    border: 1px solid #d7dde4;
    background: #f5f7f9;
    position: relative;
    border-radius: 4px;
    overflow: hidden;

}
a{
  color: #000;
  width: 100%;
  display: block;
}
.layout-logo{
    width: 100px;
    height: 30px;
    background: #5b6270;
    color: #f5f7f9;
    float: left;
    position: relative;
    top: 15px;
    left: 20px;
    text-align: center;
    line-height: 30px
}
.layout-nav{
    color: #f5f7f9;
    float: right;
    margin: 0 auto;
    margin-right: 20px;
}
.layout-nav li{
  float: right;
}
</style>